Skip to content

feat: Make TelegramClient::start() wait for TDLib initialisation#21

Open
zmij wants to merge 1 commit intodevelopfrom
dev/tg-client-to-coro
Open

feat: Make TelegramClient::start() wait for TDLib initialisation#21
zmij wants to merge 1 commit intodevelopfrom
dev/tg-client-to-coro

Conversation

@zmij
Copy link
Owner

@zmij zmij commented Dec 14, 2025

Summary

  • Remove the hardcoded 2-second sleep in exec_users_list() by implementing proper synchronisation between the main thread and TDLib's update thread
  • TelegramClient::start() now blocks until TDLib signals it's ready (via authorization state change)
  • Add comprehensive tests with mocks for the initialization synchronisation mechanism

Changes

src/tg/client.cpp

  • Add init_promise_, init_future_, and init_completed_ members to Impl
  • Add complete_initialization() helper that safely signals the promise
  • Add wait_initialized() method that blocks until TDLib is ready (with 30s timeout)
  • Modify process_authorization_state() to call complete_initialization() on meaningful state transitions (WAIT_PHONE, WAIT_CODE, WAIT_PASSWORD, or READY)
  • Update TelegramClient::start() to call wait_initialized()

src/ctl/users.cpp

  • Remove the 2-second std::this_thread::sleep_for() workaround
  • Remove now-unused <chrono> and <thread> includes

tests/tg/client_init_test.cpp (new)

  • Add MockInitSynchronizer class that mirrors the exact synchronisation logic
  • Add 11 tests covering: blocking behaviour, timeout, thread-safety, stress testing, Task integration, exception propagation

Test plan

  • All 122 tests pass (including 11 new client init tests)
  • tg-fuse users --list executes immediately without 2-second delay
  • Works correctly when already authenticated (fast path)
  • Works correctly when not authenticated (returns error promptly)

Remove the hardcoded 2-second sleep in exec_users_list() by implementing
proper synchronisation between the main thread and TDLib's update thread.

Changes:
- Add promise/future-based initialisation synchronisation to Impl
- Signal completion when auth state transitions to any ready state
  (WAIT_PHONE, WAIT_CODE, WAIT_PASSWORD, or READY)
- TelegramClient::start() now blocks until TDLib is ready
- Remove unnecessary sleep and unused includes from users.cpp
- Add comprehensive tests with mocks for the init synchronisation

The client now starts as fast as TDLib allows (typically < 1 second)
instead of always waiting 2 seconds.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant